/*
 * Dimension.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.graphics;

/**
 * Dimension Class.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Dimension {

// Public Static Fields. -------------------------------------------------------

  /**
   * Dimension(0, 0) object.
   */
  public static final Dimension ZERO_DIMESNION;

  /**
   * Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE) object.
   */
  public static final Dimension INFINITE_DIMESNION;

// Static Initialization. ------------------------------------------------------

  static {
    ZERO_DIMESNION = new Dimension();
    INFINITE_DIMESNION = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
  }

// Private Fields. -------------------------------------------------------------

  private final int width;
  private final int height;
  private int hashCode; // NOPMD

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Dimension object.
   */
  public Dimension() {
    width = 0;
    height = 0;
  }

  /**
   * Constructs new Dimension object.
   *
   * @param width the width
   * @param height the height
   */
  public Dimension(int width, int height) {
    this.width = width;
    this.height = height;
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns the width of this dimension.
   *
   * @return the width of this dimension
   */
  public int getWidth() {
    return width;
  }

  /**
   * Returns the height of this dimension.
   *
   * @return the height of this dimension
   */
  public int getHeight() {
    return height;
  }

  /**
   * Returns <tt>true</tt> if both the width and height is 0.
   *
   * @return <tt>true</tt> if both the width and height is 0
   */
  public boolean isNull() {
    return ((width == 0) && (height == 0));
  }

  /**
   * Returns <tt>true</tt> if both the width and height is equal to or greater
   * than 0.
   *
   * @return <tt>true</tt> if both the width and height is equal to or greater
   *         than 0
   */
  public boolean isValid() {
    return ((width >= 0) && (height >= 0));
  }

  /**
   * Returns enlarged dimension.
   *
   * @param dw the width enlargement
   * @param dh the height enlargement
   *
   * @return enlarged dimension
   */
  public Dimension enlarge(int dw, int dh) {
    return new Dimension(getWidth() + dw, getHeight() + dh);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if ((obj != null) && (obj.getClass() == getClass())) {
      Dimension d = (Dimension)obj;
      return ((d.getWidth() == getWidth()) && (d.getHeight() == getHeight()));
    }
    return false;
  }

  /**
   * {@inheritDoc}
   */
  public int hashCode() {
    if (hashCode == 0) {
      int hashCode = 19;
      hashCode = 37 * hashCode + getWidth();
      hashCode = 37 * hashCode + getHeight();
      this.hashCode = hashCode;
    }
    return hashCode;
  }

  /**
   * {@inheritDoc}
   */
  public String toString() {
    return getClass().getName() + "[width=" + getWidth() + ",height=" +
           getHeight() + "]";
  }

}
